﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;

namespace MyAlbum.Services
{
    public class Extension
    {
        public string DisplayName { get; set; }
        public string ProviderUrl { get; set; }
        public string FileName { get; set; }
        public string AdminFileName { get; set; }
        public NameValueCollection CustomConfiguration { get; set; }
        public List<DataRequest> DataRequest { get; set; }
        public bool Active { get; set; }

        public static Extension LoadFromXml(string xml)
        {
            Extension e = new Extension();
            XDocument doc = XDocument.Parse(xml);
            if (doc.Element("DisplayName") != null)
            {
                e.DisplayName = doc.Element("DisplayName").Value;
            }
            if (doc.Element("ProviderUrl") != null)
            {
                e.ProviderUrl = doc.Element("ProviderUrl").Value;
            }
            if (doc.Element("FileName") != null)
            {
                e.FileName = doc.Element("FileName").Value;
            }
            if (doc.Element("AdminFileName") != null)
            {
                e.AdminFileName = doc.Element("AdminFileName").Value;
            }
            if (doc.Element("Active") != null)
            {
                e.Active = (bool)doc.Element("Active");
            }
            e.CustomConfiguration = new NameValueCollection();
            if (doc.Element("CustomConfiguration") != null)
            {
                foreach (var item in doc.Element("CustomConfiguration").Descendants("Item"))
                {
                    e.CustomConfiguration.Add(item.Attribute("key").Value, item.Value);
                }
            }
            e.DataRequest = new List<DataRequest>();
            if (doc.Element("DataRequest") != null)
            {
                foreach (var item in doc.Element("DataRequest").Descendants("Item"))
                {
                    e.DataRequest.Add((DataRequest)Enum.Parse(typeof(DataRequest), item.Value, true));
                }
            }
            return e;
        }

        public static Extension Deserialize(string path)
        {
            return LoadFromXml(File.ReadAllText(path));
        }

        public void Serialize(string path)
        {
            XDocument doc = new XDocument(new XDeclaration("1.0", "utf-16", "yes"));
            XElement root = new XElement("Extension",
                new XElement("DisplayName", this.DisplayName ?? string.Empty),
                new XElement("Active", this.Active),
                new XElement("ProviderUrl", this.ProviderUrl ?? string.Empty),
                new XElement("FileName", this.FileName ?? string.Empty),
                new XElement("AdminFileName", this.AdminFileName ?? string.Empty));
            XElement config = new XElement("CustomConfiguration");
            if (this.CustomConfiguration != null)
            {
                foreach (string key in this.CustomConfiguration.Keys)
                {
                    config.Add(new XElement("Item", new XAttribute("key", key), this.CustomConfiguration[key]));
                }
            }
            root.Add(config);
            XElement dataRequest = new XElement("DataRequest");
            if (this.DataRequest != null)
            {
                foreach (var item in this.DataRequest)
                {
                    dataRequest.Add(new XElement("Item", item));
                }
            }
            root.Add(dataRequest);
            doc.Add(root);

            doc.Save(path);
        }

    }

    public enum DataRequest
    {
        Blogs,
        BlogsSummary,
        Entries,
        Categories,
        Albums,
        EntriesTags,
        CurrentPageEntries,
        LatestEntry,
        RandomEntries

    }
}
